<?php
/**
 * 平台
 * (model)
 */
namespace App\Models\Basics;

use Illuminate\Database\Eloquent\Model;

class SysPlat extends Model{

    protected $table = 'b_sys_plat';

    public $timestamps = false;

    protected $guarded = [];


    /**
     * 平台
     * @var string[]
     */
    const PLAT_NO      = 0; //无平台
    const PLAT_AMAZON  = 1; //亚马逊
    const PLAT_SHOPIFY = 2; //独立站
    const PLAT_RAKUTEN = 3; //乐天
    const PLAT_ALIEXPRESS = 4; //速卖通
    const PLAT_ALLEGRO = 5; //ALLEGRO

    const PLAT_NO_NAME      = '无';
    const PLAT_AMAZON_NAME  = 'Amazon';
    const PLAT_SHOPIFY_NAME = 'Shopify';
    const PLAT_RAKUTEN_NAME = 'Rakuten';
    const PLAT_ALIEXPRESS_NAME = 'ALIEXPRESS';
    const PLAT_ALLEGRO_NAME = 'ALLEGRO';

    //平台
    public static $_platformType = [
        self::PLAT_NO         => self::PLAT_NO_NAME,
        self::PLAT_AMAZON     => self::PLAT_AMAZON_NAME,
        self::PLAT_SHOPIFY    => self::PLAT_SHOPIFY_NAME,
        self::PLAT_RAKUTEN    => self::PLAT_RAKUTEN_NAME,
        self::PLAT_ALIEXPRESS => self::PLAT_ALIEXPRESS_NAME,
        self::PLAT_ALLEGRO    => self::PLAT_ALLEGRO_NAME
    ];

    //(JP备货)平台ID
    public static $_jpStockPlatformId = [
        self::PLAT_AMAZON,
        self::PLAT_RAKUTEN
    ];

    //可使用平台 (可以用于数据操作的平台) (例：1=>amazon)
    public static $_platformNormal = [
        self::PLAT_AMAZON  => self::PLAT_AMAZON_NAME, //Amazon
        self::PLAT_SHOPIFY => self::PLAT_SHOPIFY_NAME, //Shopify
        self::PLAT_RAKUTEN => self::PLAT_RAKUTEN_NAME  //Rakuten
    ];

    //可使用平台 (可以用于数据操作的平台)
    public static $_platformNormalId = [
        self::PLAT_AMAZON, //Amazon
        self::PLAT_SHOPIFY, //Shopify
        self::PLAT_RAKUTEN  //Rakuten
    ];

    /**
     * 平台选项
     * @param array $where
     * @return \Illuminate\Support\Collection
     */
    public static function options($where = [])
    {
        $query = $where ? self::query()->where(formatCondition($where)) : self::query();
        return $query->pluck('title','plat_id');
    }

    /**
     * 获取平台名称
     * @param string $plat_id
     * @return mixed|string
     */
    public static function getSysPlatTitle($plat_id = 0){
        $title = self::query()->where('plat_id', $plat_id)->value('title');
        return $title ?: '无';
    }

    /**
     * 获取平台id
     * @param string $title
     * @return int|mixed
     */
    public static function getSysPlatId($title = ''){
        $platId = self::query()->where('title', $title)->value('plat_id');
        return $platId ?: 0;
    }

}
